package com.bdqn.petmalldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.petmalldemo.bo.CategoryBo;
import com.bdqn.petmalldemo.common.KetaiException;
import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.entity.Category;
import com.bdqn.petmalldemo.mapper.CategoryMapper;
import com.bdqn.petmalldemo.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public List<Category> getAll() {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.orderByAsc("order_no");
        List<Category> categoryList = baseMapper.selectList(categoryQueryWrapper);
        if (CollectionUtils.isEmpty(categoryList)) {
            throw  new KetaiException(ResultCodeEnum.DATA_NULL);
        }
        return categoryList;
    }

    @Override
    public void addOrupdateCategory(CategoryBo categoryBo) {
        //校验orderNo当前分类下是否已经存在
        QueryWrapper<Category>categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.select("order_no")
                .eq("parent_Id",categoryBo.getParentId());
        List<Category> categoryList = baseMapper.selectList(categoryQueryWrapper);
        Category category = new Category();
        //当传递过来的参数中id为空的时候,执行新增;否则就执行修改
        if (StringUtils.isEmpty(categoryBo.getId())) {
            BeanUtils.copyProperties(categoryBo,category);
            //使用jdk1.8中的Strem取出序号字段
            List<Integer> orderNoList = categoryList.stream().map(Category::getOrderNo).collect(Collectors.toList());
            boolean isExist = orderNoList.contains(categoryBo.getOrderNo());
            if (isExist) {
                //当前商品分类存在重复的排列序号
                throw  new KetaiException(ResultCodeEnum.CATEGORY_ORDER_NO_IS_EXIST);
            }
            baseMapper.insert(category);
        }else{
            //先判断当前修改的对象序号是否发生改变
            Category categorybyId= this.getById(categoryBo.getId());
            if (!categoryBo.getOrderNo().equals(categorybyId.getId())) {
                //使用jdk1.8中的Strem取出序号字段
                List<Integer> orderNoList = categoryList.stream().map(Category::getOrderNo).collect(Collectors.toList());
                boolean isExist = orderNoList.contains(categoryBo.getOrderNo());
                if (isExist) {
                    //当前商品分类存在重复的排列序号
                    throw  new KetaiException(ResultCodeEnum.CATEGORY_ORDER_NO_IS_EXIST);
                }
            }
            BeanUtils.copyProperties(categoryBo,category);
            baseMapper.updateById(category);
        }

    }

    @Override
    public void updateCategory(CategoryBo categoryBo) {
        //校验orderNo当前分类下是否已经存在
        QueryWrapper<Category>categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.select("order_no")
                .eq("parent_Id",categoryBo.getParentId());
        List<Category> categoryList = baseMapper.selectList(categoryQueryWrapper);
        //使用jdk1.8中的Strem取出序号字段
        List<Integer> orderNoList = categoryList.stream().map(Category::getOrderNo).collect(Collectors.toList());
        boolean isExist = orderNoList.contains(categoryBo.getOrderNo());
        if (isExist) {
            //当前商品分类存在重复的排列序号
            throw  new KetaiException(ResultCodeEnum.CATEGORY_ORDER_NO_IS_EXIST);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryBo,category);
        baseMapper.updateById(category);
    }
}
